﻿#ifndef _TES_VECTOR2_H_
#define _TES_VECTOR2_H_

#include <assert.h>
#include <math.h>

class TesVector2
{
public:
	float x, y;

	TesVector2(float X = 0.0f, float Y = 0.0f)
	{
		x = X;
		y = Y;
	}

	TesVector2& operator = (const TesVector2& that)
	{
		x = that.x;
		y = that.y;
		return *this;
	}

	void Zero()
	{
		x = 0.0f;
		y = 0.0f;
	}

	float Length() const
	{
		return sqrtf(x * x + y * y);
	}

	float SqrLenght() const
	{
		return (x * x + y * y);
	}

	TesVector2 Normalize() const
	{
		float len = this->Length();
		assert(len != 0.0f);//除0了
		return TesVector2(x / len, y / len);
	}

	TesVector2 operator + (const TesVector2& that)
	{
		return TesVector2(x + that.x, y + that.y);
	}

	TesVector2 operator - (const TesVector2& that)
	{
		return TesVector2(x - that.x, y - that.y);
	}

	TesVector2 operator -()
	{
		TesVector2 Res = *this;
		Res.x = -Res.x;
		Res.y = -Res.y;
		return Res;
	}

	TesVector2 Projection(const TesVector2& n)
	{
		float nLength = n.Length();
		float k = (*this * n) / (nLength * nLength);
		n.Normalize();
		return n * k;
	}

	TesVector2 multiplyComponent(const TesVector2& that) 
	{
		return TesVector2(x * that.x, y * that.y);
	}

	TesVector2 operator * (float K) const
	{
		return TesVector2(x * K, y * K);
	}

	float operator * (const TesVector2& that) const
	{
		return (x * that.x + y * that.y);
	}

	TesVector2 operator / (float K) const
	{
		assert(K != 0.0f);//除0了
		return TesVector2(x / K, y / K);
	}

	bool operator == (const TesVector2& that)
	{
		return (this->x == that.x && this->y == that.y);
	}

	float Distance(const TesVector2& that) const
	{
		float SqrDeltaX = (this->x - that.x) * (this->x - that.x);
		float SqrDeltaY = (this->y - that.y) * (this->y - that.y);
		return sqrtf(SqrDeltaX + SqrDeltaY);
	}

	void GetGlVec(float Vec[2])
	{
		Vec[0] = x;
		Vec[1] = y;
	}

};

inline TesVector2 operator * (float K, const TesVector2& v)
{
	return TesVector2(v.x * K, v.y * K);
}

#endif